import 'package:flutter/material.dart';

class GKBCloseBtn extends StatelessWidget {
  final double iconSize;
  final VoidCallback? onTap;
  final Color? background;
  final Color? iconBackground;
  final Color? iconColor;
  final EdgeInsetsGeometry? margin;
  final BoxBorder? border;

  const GKBCloseBtn({
    Key? key,
    this.iconSize = 20,
    this.background,
    this.iconBackground,
    this.iconColor,
    this.margin,
    this.border,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin,
      decoration: BoxDecoration(
        color: iconBackground??Colors.black12,
        borderRadius: const BorderRadius.all(Radius.circular(25.0)),
        border: border,
      ),
      child: IconButton(
        constraints: const BoxConstraints(
          minHeight: 0,
        ),
        color: background ?? Colors.grey,
        onPressed: onTap ??
            () {
              Navigator.of(context).pop();
            },
        iconSize: iconSize,
        icon: Icon(
          Icons.close_rounded,
          color: iconColor,
        ),
        padding: const EdgeInsets.all(2),
      ),
    );
  }
}

class GKBTextButton extends StatelessWidget {
  final String title;
  final double? titleSize;
  final Color titleColor;
  final Color? borderColor;
  final Color backgroundColor;
  final Size? size; //最小尺寸
  final double radius;
  final EdgeInsets? padding;
  final BorderRadiusGeometry? borderRadius;
  final double borderWidth;
  final VoidCallback? onTap;
  final Widget? child;
  final FontWeight? fontWeight;
  final bool fixedSize;

  const GKBTextButton(
      {Key? key,
      required this.title,
      this.fixedSize = false,
      this.titleColor = Colors.white,
      this.backgroundColor = Colors.orange,
      this.radius = 0,
      this.borderRadius,
      this.borderWidth = 0,
      this.size,
      this.onTap,
      this.child,
      this.borderColor,
      this.titleSize,
      this.fontWeight,
      this.padding})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextButton(
      onPressed: onTap,
      child: child ??
          Text(
            title,
            style: TextStyle(
              color: titleColor,
              fontSize: titleSize ?? 14,
              fontWeight: fontWeight,
            ),
          ),
      style: ButtonStyle(
        minimumSize: MaterialStateProperty.all(size ?? const Size(155, 44)),
        fixedSize: fixedSize
            ? MaterialStateProperty.all(size ?? const Size(155, 44))
            : null,
        backgroundColor: MaterialStateProperty.resolveWith((states) {
          if (states.contains(MaterialState.disabled)) {
            return const Color(0xffcccccc);
          }
          return backgroundColor;
        }),
        padding: MaterialStateProperty.all(padding ?? EdgeInsets.zero),
        shape: MaterialStateProperty.all(RoundedRectangleBorder(
            side: borderWidth == 0
                ? BorderSide.none
                : BorderSide(
                    width: borderWidth,
                    color: borderColor ?? const Color(0xffF6f6f6)),
            borderRadius:
                borderRadius ?? BorderRadius.all(Radius.circular(radius)))),
        tapTargetSize: MaterialTapTargetSize.shrinkWrap,
      ),
    );
  }
}

class GKBIconTextButton extends StatelessWidget {
  final String title;
  final double? titleSize;
  final Color titleColor;
  final Color? borderColor;
  final Color backgroundColor;
  final double radius;
  final EdgeInsets? padding;
  final double borderWidth;
  final VoidCallback? onTap;
  final Widget icon;

  const GKBIconTextButton(
      {Key? key,
      required this.title,
      this.titleColor = Colors.white,
      this.backgroundColor = Colors.orange,
      this.radius = 0,
      this.borderWidth = 0,
      this.onTap,
      required this.icon,
      this.borderColor,
      this.titleSize,
      this.padding})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding:
            padding ?? const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
        decoration: BoxDecoration(
            color: backgroundColor,
            borderRadius: BorderRadius.circular(radius),
            border: borderWidth == 0
                ? null
                : Border.all(
                    width: borderWidth,
                    color: borderColor ?? const Color(0xffF6f6f6))),
        child: Row(
          children: [
            icon,
            const SizedBox(
              width: 4,
            ),
            Text(
              title,
              style: TextStyle(
                color: titleColor,
                fontSize: titleSize ?? 14,
              ),
            )
          ],
        ),
      ),
    );
  }
}

class GKBIconButton extends StatefulWidget {
  final bool select;
  final Widget icon;
  final Widget selectIcon;
  final ValueChanged<bool>? onTap;

  const GKBIconButton(
      {Key? key,
      this.select = false,
      required this.icon,
      required this.selectIcon,
      this.onTap})
      : super(key: key);

  @override
  _GKBIconButtonState createState() => _GKBIconButtonState();
}

class _GKBIconButtonState extends State<GKBIconButton> {
  bool _isSelect = false;

  @override
  void initState() {
    super.initState();
    _isSelect = widget.select;
  }

  @override
  Widget build(BuildContext context) {
    return IconButton(
        onPressed: () {
          _isSelect = !_isSelect;
          setState(() {});
          widget.onTap?.call(_isSelect);
        },
        icon: _isSelect ? widget.selectIcon : widget.icon);
  }
}
